package com.fh.common.system.log.entity;

import java.io.Serializable;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.format.annotation.DateTimeFormat;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fh.common.util.JsonUtils;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

/**
 * 
 * @author junqiu
 *
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class RequestLog  implements Serializable {

	private static final long serialVersionUID = 1L;

	/**
	 * id
	 */
	@TableId(type = IdType.ID_WORKER_STR)
	private String id;
	
	/**
	 * sessionId
	 */
	private String sessionId;
	
	/**
	 * 请求账号
	 */
	private String requestAccount;
	
	/**
	 * 请求uri
	 */
	private String requestUri;
	
	/**
	 * 请求url
	 */
	private String requestUrl;
	
	/**
	 * 请求的方法
	 */
	private String requestMethod;
	
	/**
	 * 请求的参数
	 */
	private String requestParams;
	
	/**
	 * body参数
	 */
	private String requestBodyParams;
	
	/**
	 * 请求时间
	 */
	@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
	@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	private Date requestTime;
	
	public RequestLog init(HttpServletRequest request) {
		this.setRequestMethod(request.getMethod());
		this.setRequestUri(request.getRequestURI());
		this.setRequestUrl(request.getRequestURL().toString());
		this.setRequestTime(new Date());
		this.setSessionId(request.getSession().getId());
		this.setRequestParams(JsonUtils.obj2String(request.getParameterMap()));
		this.setRequestBodyParams("");
		return this;
	}
	

}
